/*
 * 
 * author     date                    function                 purpose
 * wuwentao   20111122             base structure               */


package com.badlogicgames.framwork;

import java.util.List;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GLCommon;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.math.Vector3;
import com.badlogicgames.oceanballwar.Assets;
import com.badlogicgames.oceanballwar.wolf.Wolf;


public class GameScreen extends Screen {
	/*game status*/
	static final int GAME_READY = 0;//
	static final int GAME_RUNNING = 1;
	static final int GAME_PAUSED = 2;
	static final int GAME_LEVEL_END = 3;//go through all levels of the game
	static final int GAME_OVER = 4;

	int state;
	OrthographicCamera guiCam;
	Vector3 touchPoint;
	SpriteBatch batcher;
	WorldRenderer renderer;
	Wolf wolf;
	
	public GameScreen (Game game) {
		super(game);
		state = GAME_READY;
		guiCam = new OrthographicCamera(GameConstant.CAMERA_WIDTH, GameConstant.CAMERA_HEIGHT);
		guiCam.position.set(GameConstant.CAMERA_WIDTH / 2, GameConstant.CAMERA_HEIGHT / 2, 0);
		touchPoint = new Vector3();
		batcher = new SpriteBatch();
		
		renderer = new WorldRenderer(batcher);
		initItems();
	}

	@Override
	public void update (float deltaTime) {
		if (deltaTime > 0.1f) deltaTime = 0.1f;

		switch (state) {
		case GAME_READY:
			updateReady();
			break;
		case GAME_RUNNING:
			updateRunning(deltaTime);
			break;
		case GAME_PAUSED:
			updatePaused();
			break;
		case GAME_LEVEL_END:
			updateLevelEnd();
			break;
		case GAME_OVER:
			updateGameOver();
			break;
		}
	}

	private void updateReady () {
	//	if (Gdx.input.justTouched()) {
	//		state = GAME_RUNNING;
	//	}
	}

	private void updateRunning (float deltaTime) {
		
	}

	private void updatePaused () {
		
	}

	private void updateLevelEnd () {
	}

	private void updateGameOver () {
		if (Gdx.input.justTouched()) {
			game.setScreen(new MainMenuScreen(game));
		}
	}

	@Override
	public void present (float deltaTime) {
		GLCommon gl = Gdx.gl;
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		gl.glEnable(GL10.GL_TEXTURE_2D);

		renderer.render();

		guiCam.update();
		batcher.setProjectionMatrix(guiCam.combined);
		batcher.enableBlending();
		batcher.begin();
		switch (state) {
		case GAME_READY:
			presentReady();
			break;
		case GAME_RUNNING:
			presentRunning();
			break;
		case GAME_PAUSED:
			presentPaused();
			break;
		case GAME_LEVEL_END:
			presentLevelEnd();
			break;
		case GAME_OVER:
			presentGameOver();
			break;
		}
		Assets.present(Gdx.graphics.getDeltaTime());
		//wolf.walkStraight(Gdx.graphics.getDeltaTime());//update statetime
		batcher.end();
	}

	private void presentReady () {		
		//TextureRegion frame = Assets.wolfAnim.getLoopKeyFrame(wolf.stateTime);
		//batcher.draw(frame, wolf.position.x, wolf.position.y);
		Assets.draw(batcher);
	}

	private void presentRunning () {
	}

	private void presentPaused () {
	}

	private void presentLevelEnd () {
	}

	private void presentGameOver () {
	}
	
	public void initItems()
	{
		//wolf = new Wolf(400, 400);
		List<AtlasRegion> lA = Assets.androidatlas.getRegions();
		TextureRegion[] tmpFrame = new TextureRegion[lA.size()];
		for(int i=0; i<lA.size(); i++)
			tmpFrame[i] = lA.get(i);
		//Assets.wolfAnim = new Animation(0.15f, tmpFrame);		
		Assets.initalAnimation(0.15f, tmpFrame);
	}
	
	@Override
	public void pause () {
		if (state == GAME_RUNNING) state = GAME_PAUSED;
	}

	@Override
	public void resume () {
	}

	@Override
	public void dispose () {
	}
}
